package strategy.demo;

public class QuickSortStrategy implements SortStrategy {
    @Override
    public void sort(int[] arr) {

        sort(arr, 0, arr.length - 1);

    }

    private void sort(int[] arr, int left, int right) {
        if (left >= right) return;
        int pivot = arr[left];
        int i = left, j = right;
        while (i < j) {
            while (i < j && arr[j] >= pivot) j--;
            while (i < j && arr[i] <= pivot) i++;
            if (i >= j) break;
            int t = arr[i];
            arr[i] = arr[j];
            arr[j] = t;
        }
        if (i > left) {
            int t = arr[left];
            arr[left] = arr[i];
            arr[i] = t;
        }
        int p = i;
        sort(arr, left, p - 1);
        sort(arr, p + 1, right);

    }


}
